import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_base/statemanagement_demo/components/goods_item.dart';
import 'package:flutter_base/statemanagement_demo/models/goods_entity.dart';
import 'package:flutter_base/statemanagement_demo/states/cart_model.dart';

class MyCartPage extends StatelessWidget {
  const MyCartPage({super.key});

  @override
  Widget build(BuildContext context) {
    List<GoodsEntity> goodsEntities = context.read<CartModel>().items;

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: () => Navigator.of(context).pop(),
          icon: const Icon(Icons.arrow_back_ios),
        ),
        title: const Text('我的购物车'),
      ),
      body: Container(
        color: Colors.grey[50],
        child: Consumer<CartModel>(
          builder: (context, cart, child) {
            return ListView.builder(
              itemBuilder: (context, index) => GoodsItem(goodsEntities[index]),
              itemCount: goodsEntities.length,
            );
          },
        ),
      ),
      bottomNavigationBar: Container(
        height: 70,
        width: MediaQuery.of(context).size.width,
        padding: const EdgeInsets.all(10.0),
        alignment: Alignment.center,
        decoration: const BoxDecoration(
          border: Border(top: BorderSide(color: Colors.grey, width: 1.0)),
          color: Colors.white,
        ),
        child: Column(
          children: <Widget>[
            Expanded(
              child: Row(
                children: [
                  Expanded(
                    child: Text(
                      '总计：￥${context.read<CartModel>().totalPrice}',
                      style: const TextStyle(fontSize: 18.0),
                    ),
                  ),
                  TextButton(
                    onPressed: () {},
                    child: const Text('结算', style: TextStyle(fontSize: 18.0)),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
